		TITLE	ALLOSECT - Copyright (c) SLR Systems 1994

		INCLUDE MACROS
		INCLUDE	SEGMENTS
		INCLUDE	MODULES
		INCLUDE	CLASSES
		INCLUDE	SECTIONS

		PUBLIC	SECTION_ALLOCATION


		.DATA

		EXTERNDEF	OUTBUF:BYTE

		EXTERNDEF	CURN_TYPE_MODULE:DWORD,CURN_SYMBOL_MODULE:DWORD,FIRST_SEGMENT_GINDEX:DWORD,FIRST_MODULE_GINDEX:DWORD
		EXTERNDEF	FIRST_SECTION_GINDEX:DWORD,CURN_TYPE_SEGMOD_GINDEX:DWORD,CURN_SYMBOL_SEGMOD_GINDEX:DWORD

		EXTERNDEF	CLASS_GARRAY:STD_PTR_S,SEGMOD_GARRAY:STD_PTR_S,SEGMENT_GARRAY:STD_PTR_S,SECTION_GARRAY:STD_PTR_S
		EXTERNDEF	MODULE_GARRAY:STD_PTR_S,MDB_GARRAY:STD_PTR_S


		.CODE	MIDDLE_TEXT

		EXTERNDEF	MOVE_AXSI_STG:PROC,MOVE_VNAME:PROC,CBTA16:PROC,FLUSH_OUTBUF:PROC,GET_SM_MODULE:PROC,MOVE_PNAME:PROC


SECTION_ALLOCATION	PROC
		;
		;SCAN ALL SEGMODS, ASSIGNING TO SECTIONS
		;
		MOV	ESI,FIRST_SEGMENT_GINDEX
		JMP	L8$

L0$:
		JMP	DO_CV_SECTION_ALLOC

L1$:
		CONVERT	ESI,ESI,SEGMENT_GARRAY
		ASSUME	ESI:PTR SEGMENT_STRUCT

		MOV	AL,[ESI]._SEG_TYPE
		MOV	ECX,[ESI]._SEG_NEXT_SEG_GINDEX

		TEST	AL,MASK SEG_CV_TYPES1 + MASK SEG_CV_SYMBOLS1	;CODEVIEW DIFFERENT
		JNZ	L0$

		MOV	EAX,[ESI]._SEG_FIRST_SEGMOD_GINDEX
		PUSH	ECX

		TEST	EAX,EAX
		JZ	L71$
L2$:
		CALL	ASSIGN_SEGMOD

		TEST	EAX,EAX
		JNZ	L2$
L71$:
		POP	ESI
L8$:
		TEST	ESI,ESI
		JNZ	L1$
L9$:
		RET

SECTION_ALLOCATION	ENDP


ASSIGN_SEGMOD	PROC	NEAR
		;
		;EAX IS A SEGMOD, ASSIGN IT TO A SECTION PLEASE...
		;
		MOV	ESI,EAX
		MOV	ECX,EAX
		CONVERT	ESI,ESI,SEGMOD_GARRAY
		ASSUME	ESI:PTR SEGMOD_STRUCT

		XOR	EDX,EDX

		MOV	AL,[ESI]._SM_PLTYPE
		MOV	EBX,[ESI]._SM_NEXT_SEGMOD_GINDEX

		MOV	[ESI]._SM_NEXT_SEGMOD_GINDEX,EDX
		TEST	AL,MASK SECTION_ASSIGNED

		MOV	EDI,[ESI]._SM_SECTION_GINDEX
		JNZ	L3$

		PUSH	EBX
		CALL	ASSIGN_SECTION

		POP	EBX
		MOV	EDI,[ESI]._SM_SECTION_GINDEX
L3$:
		;
		;ADD THIS SEGMOD TO ITS SECTION LIST
		;
		CONVERT	EDI,EDI,SECTION_GARRAY
		ASSUME	EDI:PTR SECTION_STRUCT

		MOV	EAX,[EDI]._SECT_LAST_SEGMOD_GINDEX
		MOV	[EDI]._SECT_LAST_SEGMOD_GINDEX,ECX

		TEST	EAX,EAX
		JZ	L4$
		CONVERT	EAX,EAX,SEGMOD_GARRAY
		ASSUME	EAX:PTR SEGMOD_STRUCT

		MOV	[EAX]._SM_NEXT_SEGMOD_GINDEX,ECX
		MOV	EAX,EBX

		RET

L4$:
		MOV	[EDI]._SECT_FIRST_SEGMOD_GINDEX,ECX
		MOV	EAX,EBX

		RET

ASSIGN_SEGMOD	ENDP


DO_CV_SECTION_ALLOC	PROC
		;
		;STARTING CODEVIEW SEGMENTS, ASSIGN TYPES THEN LOCALS PER MODULE...
		;
		;START WITH MODULE 1, THRU MODULE N (TILL NO MORE SEGMODS LEFT)
		;
		BITT	CODEVIEW_FLAG
		JZ	L9$
		MOV	ESI,FIRST_MODULE_GINDEX
		JMP	L8$

L1$:
		CONVERT	ESI,ESI,MODULE_GARRAY
		ASSUME	ESI:PTR MODULE_STRUCT

		MOV	EDX,[ESI]._M_MDB_GINDEX
		MOV	ESI,[ESI]._M_NEXT_MODULE_GINDEX

		TEST	EDX,EDX
		JZ	L8$

		CONVERT	EDX,EDX,MDB_GARRAY
		ASSUME	EDX:PTR MDB_STRUCT

		PUSH	ESI

		MOV	EAX,[EDX]._MD_CV_TYPES_GINDEX
		MOV	EBX,[EDX]._MD_CV_SYMBOLS_GINDEX

		TEST	EAX,EAX
		JZ	L3$

		PUSH	EBX
		CALL	ASSIGN_SEGMOD

		POP	EBX
L3$:
		TEST	EBX,EBX
		JZ	L4$

		MOV	EAX,EBX
		CALL	ASSIGN_SEGMOD
L4$:
		POP	ESI
L8$:
		TEST	ESI,ESI
		JNZ	L1$
L9$:
		RET

DO_CV_SECTION_ALLOC	ENDP


ife	any_overlays

ASSIGN_SECTION	PROC	NEAR
		;
		;ASSIGN THIS TO SECTION ZERO
		;
		ASSUME	ESI:PTR SEGMOD_STRUCT

		MOV	AL,[ESI]._SM_PLTYPE
		MOV	EBX,FIRST_SECTION_GINDEX

;		MOV	EDI,[ESI]._SM_BASE_SEG_GINDEX
;		CONVERT	EDI,EDI,SEGMENT_GARRAY
;		ASSUME	EDI:PTR SEGMENT_STRUCT

		OR	AL,MASK SECTION_ASSIGNED + MASK LEVEL_0_SECTION
		MOV	[ESI]._SM_SECTION_GINDEX,EBX

		MOV	[ESI]._SM_PLTYPE,AL

;		MOV	[EDI]._SEG_SECTION_GINDEX,EBX

		RET

ASSIGN_SECTION	ENDP

else

ASSIGN_SECTION	PROC	NEAR
		;
		;CX  DS:SI IS SEGMOD, ASSIGN TO A SECTION PLEASE
		;RETURN WITH DS:SI VALID...
		;
		OR	[SI]._SM_PLTYPE,MASK SECTION_ASSIGNED
		PUSHM	CX
		CMP	[SI]._SM_SECTION_GINDEX,0	;REFERENCED BY SOME SECTION?
		JZ	5$
		TEST	[SI]._SM_PLTYPE,MASK LEVEL_0_SECTION	;REFERENCED BY A LEVEL-0 SECTION?
		JZ	2$
1$:
		ADD	SP,2				;YES, PUT ME IN SECTION 1

		CALL	ASSIGN_FIRST_SECTION
		RET

5$:
		;
		;NO SECTION YET
		;
		BITT	DOING_OVERLAYS
		JZ	1$
		;
		;SEE IF SEGMENT WAS ASSIGNED ANYWHERE YET...
		;
		MOV	AX,[SI]._SM_BASE_SEG_GINDEX
		CONVERT_GINDEX_AX_ESDI	SEGMENT_GARRAY
		MOV	AX,ES:[DI]._SEG_SECTION_GINDEX
		OR	AX,AX
		JNZ	58$
		;
		;NO, TRY OTHER SEGMODS IN SEGMENT
		;
52$:
		MOV	AX,[SI]._SM_NEXT_SEGMOD_GINDEX
		OR	AX,AX
		JZ	57$
		CONVERT_GINDEX_AX_DSSI	SEGMOD_GARRAY
		MOV	CX,[SI]._SM_SECTION_GINDEX
		OR	CX,CX
		JZ	52$
		POP	AX
		PUSH	AX
		CONVERT_GINDEX_AX_DSSI	SEGMOD_GARRAY
		MOV	[SI]._SM_SECTION_GINDEX,CX
		JMP	2$

57$:
		;
		;NO SEGMODS ASSIGNED...
		;
		POP	AX
		PUSH	AX
		CONVERT_GINDEX_AX_DSSI	SEGMOD_GARRAY
		;
		;NOTHING IN LIST ASSIGNED OR REFERENCED, FORCE ROOT
		;
		CALL	ASSIGN_FIRST_SECTION

		PUSH	SI
		LEA	DI,OUTBUF
		FIXES
		LEA	SI,UNREF
		MOV	AX,SEG UNREF
		CALL	MOVE_AXSI_STG
		POP	SI
		JMP	4$

58$:
		;
		;SOME OTHER SEGMOD CAUSED IT...
		;
		MOV	[SI]._SM_SECTION_GINDEX,AX
		JMP	3$

2$:
		;
		;REFERENCED BY A NON-LEVEL 0, SO PUT ME AND MY BASE THERE
		;
		CALL	ASSIGN_BASE_SEGMENT
		;
		;
		;
3$:
		CMP	[SI]._SM_SECTION_GINDEX,1
		JNZ	31$
		OR	[SI]._SM_PLTYPE,MASK LEVEL_0_SECTION
31$:
		LEA	DI,OUTBUF
		FIXES
4$:
		BITT	INFORMATION_FLAG
		JZ	7$
		;
		;IGNORE IF A ZERO-LENGTH SEGMOD...
		;
		MOV	AX,[SI]._SM_LEN.LW
		OR	AX,[SI]._SM_LEN.HW
		JZ	7$
		CALL	DO_REPORT
7$:
		POP	CX
		MOV	AX,CX
		CONVERT_GINDEX_AX_DSSI	SEGMOD_GARRAY
		RET

ASSIGN_SECTION	ENDP


ASSIGN_FIRST_SECTION	PROC	NEAR
		;
		;ASSIGN THIS TO SECTION ZERO
		;
		MOV	AX,[SI]._SM_BASE_SEG_GINDEX
		CONVERT_GINDEX_AX_ESDI	SEGMENT_GARRAY
		MOV	AX,1
		MOV	[SI]._SM_SECTION_GINDEX,AX
		OR	[SI]._SM_PLTYPE,MASK LEVEL_0_SECTION
		MOV	ES:[DI]._SEG_SECTION_GINDEX,AX
		RET

ASSIGN_FIRST_SECTION	ENDP

endif

if	any_overlays

ASSIGN_BASE_SEGMENT	PROC	NEAR
		;
		;ASSIGN MY SECTION TO MY BASE SEGMENT
		;
		MOV	AX,[SI]._SM_BASE_SEG_GINDEX
		CONVERT_GINDEX_AX_ESDI	SEGMENT_GARRAY
		MOV	AX,[SI]._SM_SECTION_GINDEX
		MOV	ES:[DI]._SEG_SECTION_GINDEX,AX
		RET

ASSIGN_BASE_SEGMENT	ENDP

DO_REPORT	PROC	NEAR
		;
		;REPORT THE ASSIGNMENT
		;CLASS:SEGMENT IN MODULE NAME TO SECTION #N NAME
		;
		PUSH	[SI]._SM_SECTION_GINDEX
		CALL	GET_SM_MODULE
		PUSH	AX
		MOV	AX,[SI]._SM_BASE_SEG_GINDEX
		PUSH	AX
		CONVERT_GINDEX_AX_DSSI	SEGMENT_GARRAY
		MOV	AX,[SI]._SEG_CLASS_GINDEX
		CONVERT_GINDEX_AX_DSSI	CLASS_GARRAY
		LEA	SI,[SI]._C_TEXT
		CALL	MOVE_PNAME
		MOV	AL,':'
		STOSB
		POP	AX
		CONVERT_GINDEX_AX_DSSI	SEGMENT_GARRAY
		LEA	SI,[SI]._SEG_TEXT
		CALL	MOVE_PNAME
		MOV	AX,SEG IN_MODULE
		LEA	SI,IN_MODULE
		CALL	MOVE_AXSI_STG
		POP	AX
		CONVERT_GINDEX_AX_DSSI	MODULE_GARRAY
		LEA	SI,[SI]._M_TEXT
		CALL	MOVE_PNAME
		MOV	AX,SEG TO_SECTION
		LEA	SI,TO_SECTION
		CALL	MOVE_AXSI_STG
		POP	AX
		PUSH	AX
		CALL	CBTA16
		MOV	AL,' '
		STOSB
		POP	AX
		CONVERT_GINDEX_AX_DSSI	SECTION_GARRAY
		LDS	SI,[SI]._SECT_NAME
		MOV	CX,DS
		JCXZ	3$
		CALL	MOVE_VNAME
3$:
		CALL	FLUSH_OUTBUF
		RET

DO_REPORT	ENDP

ife	data_in_codeseg

		.DATA

endif

IN_MODULE	DB	LENGTH IN_MODULE-1,' in module '
TO_SECTION	DB	LENGTH TO_SECTION-1,' to Section # '
UNREF		DB	LENGTH UNREF-1,'Unreferenced '

endif


		END

